export function GET(opt) {
  return request('GET', opt)
}
export function POST(opt) {

  return request('POST', opt)
}

function request(method = 'GET', opt = {}) {
  let { url = '', data = null } = opt
  // console.log(url, method, data)
  return new Promise((resolve, reject) => {
    uni.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data,
      method,
      url: url,
      success: function (res) {
        if (res.statusCode === 200) {
          if (res.data) {
            resolve(res.data)
          } else {
            resolve(res.data)
          }
        } else {
          uni.showToast({
            title: '错误',
            icon: 'none'
          })
          reject(res.data)
        }
      },
      fail: function (res) {
        uni.showToast({
          title: '服务器错误',
          icon: 'none'
        })
        console.log(JSON.stringify(res))
      },
      complete: function (res) {
        // console.log('调用结束，成功、失败都会执行')
      },
    })
  })
}

export function showModal(title, content) {
  uni.showModal({
    title,
    content,
    showCancel: false
  })
}

export function showSuccess(text) {
  uni.showToast({
    title: text,
    icon: 'success'
  })
}